package cn.bupt.adt.controller;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.FeeConf;
import cn.bupt.adt.entity.autogenerate.Institution;
import cn.bupt.adt.model.DoctorSearchModel;
import cn.bupt.adt.model.InstitutionModel;
import cn.bupt.adt.service.IFeeConfService;
import cn.bupt.adt.service.IInstitutionService;
import cn.bupt.adt.util.NetDataAccessUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/institutionManagement")
public class InstitutionController {

    @Autowired
    IInstitutionService institutionService;
    @Autowired
    IFeeConfService feeConfService;

    @RequestMapping("/index/{pageNum}")
    public ModelAndView doctorIndex(@PathVariable("pageNum") int pageNum, DoctorSearchModel doctorSearchModel) {
        ModelAndView mv = new ModelAndView(Constants.INSTITUTION_INDEX);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<InstitutionModel> institutions = null;
        if (doctorSearchModel.getSearchMethod() == null)
            institutions = institutionService.getAllInstitutions();
        else {
            if (doctorSearchModel.getSearchMethod().equals(Constants.BY_NAME)) {
                institutions = institutionService.getInstitutionByFuzzyName(doctorSearchModel.getContent());
            } else {
                institutions = institutionService.getInstitutionByPhone(doctorSearchModel.getContent());
            }
            mv.addObject("searchMethod", doctorSearchModel.getSearchMethod());
            mv.addObject("content", doctorSearchModel.getContent());
        }
        mv.addObject("currentPage", page.getPageNum());
        mv.addObject("allPages", page.getPages());
        mv.addObject("institutions", institutions);
        return mv;
    }

    @RequestMapping("/addInstitution")
    public ModelAndView addInstitution() {
        ModelAndView mv = new ModelAndView(Constants.ADD_INSTITUTION);
        return mv;
    }

    @RequestMapping("/addInstitutionPost")
    public synchronized ModelAndView addInstitutionPost(Institution ins, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("forward:/institutionManagement/index/1");
        String oper_message = null;
        if (institutionService.addInstitution(ins, request))
            oper_message = "添加机构成功！";
        else
            oper_message = "添加机构失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/editPhone")
    public ModelAndView editPhone(Institution ins) {
        ModelAndView mv = new ModelAndView("forward:/doctorManagement/index/1");
        String oper_message = null;
        if (institutionService.modifyInstitution(ins))
            oper_message = "修改电话成功！";
        else
            oper_message = "修改电话失败！可能是网络原因，也可能是已存在相同的电话，请检查";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/deleteIns/{id}")
    public ModelAndView deleteIns(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView("forward:/institutionManagement/index/1");
        String oper_message = null;
        if (institutionService.deleteInstitutionById(id))
            oper_message = "删除机构成功！";
        else
            oper_message = "删除机构失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/insDetail/{id}")
    public ModelAndView insDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.INSTITUTION_DETAIL);
        InstitutionModel im = institutionService.getInstitutionById(id);
        mv.addObject("item", im);
        return mv;
    }

    @RequestMapping("/modifyIns")
    public ModelAndView modifyIns(HttpServletRequest request, Institution ins) {
        ModelAndView mv = new ModelAndView("forward:/institutionManagement/insDetail/" + ins.getId());
        String oper_message = null;
        if (institutionService.modifyInstitution(ins, request))
            oper_message = "保存成功！";
        else
            oper_message = "保存失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/feeConf/add")
    @ResponseBody
    public NetDataAccessUtil addFeeConf(FeeConf feeConf) {
        if (feeConf.getInstitutionId() == null || feeConf.getUnitPrice() == null || feeConf.getBase() == null
                || feeConf.getCompl() == null || feeConf.getEmergency() == null || feeConf.getSpecial() == null) {
            return NetDataAccessUtil.error().resultDesp("必填参数存在空值");
        }
        Integer id = feeConfService.insert(feeConf);
        feeConf.setId(id);
        return NetDataAccessUtil.success().content(feeConf).resultDesp("费用配置成功");
    }

}
